<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="楼栋" prop="buildingType">
        <el-select v-model="queryParams.buildingType" placeholder="请选择楼栋" clearable>
          <el-option
            v-for="dict in dict.type.building_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间号" prop="roomNumber">
        <el-input
          v-model="queryParams.roomNumber"
          placeholder="请输入房间号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间类型）" prop="roomType">
        <el-select v-model="queryParams.roomType" placeholder="请选择房间类型）" clearable>
          <el-option
            v-for="dict in dict.type.room_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="房间容纳人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入房间容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="每晚价格" prop="pricePerNight">
        <el-input
          v-model="queryParams.pricePerNight"
          placeholder="请输入每晚价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="房间状态）" prop="roomStatus">
        <el-select v-model="queryParams.roomStatus" placeholder="请选择房间状态）" clearable>
          <el-option
            v-for="dict in dict.type.room_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(0)"
        >预览A栋情况
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(1)"
        >预览B栋情况
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="preview(2)"
        >预览C栋情况
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="roomsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="房间ID，主键，自增" align="center" prop="roomId"/>
      <el-table-column label="楼栋" align="center" prop="buildingType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.building_type" :value="scope.row.buildingType"/>
        </template>
      </el-table-column>
      <el-table-column label="楼层" align="center" prop="floor"/>
      <el-table-column label="房间号" align="center" prop="roomNumber"/>
      <el-table-column label="房间类型）" align="center" prop="roomType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_type" :value="scope.row.roomType"/>
        </template>
      </el-table-column>
      <el-table-column label="房间容纳人数" align="center" prop="capacity"/>
      <el-table-column label="每晚价格" align="center" prop="pricePerNight"/>
      <el-table-column label="房间设施" align="center" prop="amenities"/>
      <el-table-column label="房间图片" align="center" prop="imgUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imgUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="房间状态）" align="center" prop="roomStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.room_status" :value="scope.row.roomStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="用户姓名" align="center" prop="userName"/>
      <el-table-column label="用户身份证" align="center" prop="userIdCard"/>
      <el-table-column label="用户入住日期" align="center" prop="userCheckinDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.userCheckinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          > 定住
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改房间管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入姓名"/>
        </el-form-item>
        <el-form-item label="用户身份证" prop="userIdCard">
          <el-input v-model="form.userIdCard" placeholder="请输入用户身份证"/>
        </el-form-item>
        <el-form-item label="用户入住日期" prop="userCheckinDate">
          <el-date-picker clearable
                          v-model="form.userCheckinDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择用户入住日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="checkout.open=true">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="checkout.title" :visible.sync="checkout.open" width="500px" append-to-body>
      <!--支付宝收款码-->
      <div style="text-align: center">请支付{{ form.pricePerNight * 7 }}元 押金</div>
      <image-preview src="https://ts1.cn.mm.bing.net/th/id/R-C.1e5871c9b34fc3bf72af08f3ca9cf38c?rik=izMAIOABpwShEA&riu=http%3a%2f%2fcache.mobanma.com%2fUploads%2fpro%2fcover%2f2017%2f02%2f08%2f201702082213457740FemOPA.png&ehk=c%2bvuGxzjrtyPEFLGt%2fo%2fARfphuxmo7mlhEp0x7tZshs%3d&risl=&pid=ImgRaw&r=0" :width="50" :height="50"/>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="checkout.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!--// 全屏-->
    <el-dialog :title="pv.id" :visible.sync="pv.open" :fullscreen="true" append-to-body>
      <el-row v-for="(lc, index) in 10" :key="index">
        <el-col span="2" v-for="(item, index) in pv.list.filter((item) => { return item.floor === lc})">
          <el-card class="box-card" v-if="item.roomStatus === 0" style="background-color: rgba(12,229,238,0.5)">
            <div slot="header" class="clearfix">
              <span>{{ lc }}楼 </span>
              <el-button style="float: right; padding: 3px 0" type="text"></el-button>
            </div>
            <div class="text item">
              <!--显示入住状态-->
              <span>可入住 房间号为 {{ item.roomNumber }}</span>
            </div>
          </el-card>
          <el-card class="box-card" v-if="item.roomStatus === 1" style="background-color: rgba(152,4,185,0.5)">
            <div slot="header" class="clearfix">
              <span>{{ lc }}楼 </span>
              <el-button style="float: right; padding: 3px 0" type="text"></el-button>
            </div>
            <div class="text item">
              <!--显示入住状态-->
              <span>当前房间已入住 </span>
              <h>房间号为 {{ item.roomNumber }}</h>
            </div>
          </el-card>

        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import {listRooms, getRooms, delRooms, addRooms, updateRooms} from "@/api/hotefl/rooms";

export default {
  name: "Rooms",
  dicts: ['building_type', 'room_type', 'room_status'],
  data() {
    return {
      // 预览
      pv: {
        id: '',
        open: false,
        list:[]
      },
      // checkout
      checkout: {
        money: 0,
        id: '',
        open: false,
        title: "扫码付款",
        imgUrl: this.$store.getters.avatar,
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间管理表格数据
      roomsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    preview(typeId) {
      this.pv.id = typeId;
      listRooms(this.queryParams).then(response => {
        this.queryParams.buildingType = typeId;
        this.queryParams.pageSize = 1000;
        this.pv.list = response.rows.filter(item => { return item.buildingType === typeId})
        this.pv.open = true;
      });
    },
    /** 查询房间管理列表 */
    getList() {
      this.loading = true;
      listRooms(this.queryParams).then(response => {
        this.roomsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        roomId: null,
        buildingType: null,
        floor: null,
        roomNumber: null,
        roomType: null,
        capacity: null,
        pricePerNight: null,
        amenities: null,
        imgUrl: null,
        roomStatus: null,
        userId: null,
        userName: null,
        userIdCard: null,
        userCheckinDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "入住填写信息";
      });
    },
    toCheckout(row) {
      const roomId = row.roomId || this.ids
      getRooms(roomId).then(response => {
        this.form = response.data;
        this.checkout.open = true;
        this.checkout.id = roomId;
        this.checkout.money = roomId;
        this.checkout.title = "扫描支付";
        // 计算金额 当前时间- 入住时间 * 房间价格
        let number = new Date().getTime() - new Date(this.form.userCheckinDate).getTime();
        // 转天数
        number = number / (1000 * 60 * 60 * 24);
        this.checkout.money = number * this.form.pricePerNight;
      });
      this.reset();
    },
    /** 提交按钮 */
    submitForm() {
      this.form.userId = this.$store.getters.userId;
      this.form.roomStatus = 1;
      if (this.form.roomId != null) {
        updateRooms(this.form).then(response => {
          this.$modal.msgSuccess("入住成功");
          this.open = false;
          this.checkout.open = false;
          this.getList();
        });
      }
    },
  }
};
</script>
